< previous page page_343 next page >

Page 343
The lpString parameter needs to be passed the address of a Unicode string. The StrPtr function can be used to obtain the address of the internal Visual Basic string, which is a Unicode string. The following code from the ByValSt.vbp example (on the CD that comes with this book) sets the form's caption using the Unicode entry point declaration for SetWindowText shown above.
Private Sub cmdSetWindowText_Click()
   Dim s$
   s$ = "New Text" & chr$(0)
   Call SetWindowText(hwnd, StrPtr(s$))
End Sub
Why do we add the NULL character at the end of the variable s$? The truth is that this may not be necessary. It is critically important that the string passed to the API function be NULL-terminated. When you use this technique, you are passing a pointer to the string as it is stored internally in Visual Basic, and as you saw earlier, a BSTR does not have a NULL termination character. In most cases, it turns out that there are a couple of NULL bytes after strings, but there is no guarantee that this will always be the case. So you should explicitly add the NULL character just to be safe.
Keep in mind also that while the SetWindowTextW entry point exists under Windows95/98, it does not do anything. Most Unicode entry points under Windows 95/98 simply return an error result.
This covers string parameters in a nutshell. Handling string parameters can be one of the more complex issues with API and DLL calls, and many of the puzzles in this book will deal with techniques for handling them.

 
< previous page page_343 next page >